﻿using System;
using System.IO;
using System.Web.UI;
using SVTTranslateTextEngine.Core;
using SVTTranslateTextEngine.TestWeb.SVTTranslateTextEngine;

namespace SVTTranslateTextEngine.TestWeb
{
    public partial class TestFileToFile : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnTranslate_Click(object sender, EventArgs e)
        {
            var startDate = DateTime.Now;
            var ttws = new TTWebService ();
            var dirPath = ttws.GetSourceContextFileSharePath();
            if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath)) return;
            var filePath = dirPath + "\\" + Guid.NewGuid() + ".txt";
            var isTransfer = new FileAccessHelper().WriteBytesContext(fuSourceContext.FileBytes, filePath);
            if (!isTransfer || !File.Exists(filePath)) return;
            var input = new FileInputParam
                            {
                                szVendorName = "GOOGLE",
                                szFromLang = ddlFrom.SelectedValue,
                                szToLang = ddlTo.SelectedValue,
                                szFromFilename = filePath
                            };
            var fileResult = ttws.TranslateFileToFile(input);
            if (string.IsNullOrEmpty(fileResult.szErrorMessage) && string.IsNullOrEmpty(fileResult.szExceptionMessage))
            {
                txtTrg.Text = new FileAccessHelper().ReadTextContext(fileResult.szToFilename);
                lblMsg.Text = "The target context file path is :<br />" +
                              fileResult.szToFilename + "<br /><br />";
                if (File.Exists(fileResult.szToFilename))
                    File.Delete(fileResult.szToFilename);
            }
            else
                lblMsg.Text = fileResult.szErrorMessage + (fileResult.szExceptionMessage ?? "") + "<br /><br />";
            lblMsg.Text += "Uses Time : [" + (DateTime.Now - startDate).TotalMilliseconds + "]ms";
        }
    }
}